<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <template>
        <el-header height="15px">车厢管理
        </el-header>
        <el-divider></el-divider>
        <el-form :inline="true" :model="searchForm" class="demo-form-inline" >
            <el-form-item label="列车号" prop="trainNum">
                <el-input v-model="searchForm.trainNum" ></el-input>
            </el-form-item>
            <el-form-item label="车厢号" prop="coachNum">
                <el-input v-model="searchForm.coachNum" ></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="selectAgain">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" plain @click="deleteByIds" style="margin-left: 60%">批量删除</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" plain @click="dialogFormVisible = true" style="margin-left: 80%">新增</el-button>
            </el-form-item>
        </el-form>

        <el-table
                :data="tableData"
                stripe
                style="width: 100%"
                ref="multipleTable"
                tooltip-effect="dark"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    prop="trainNum"
                    label="列车号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="coachNum"
                    label="车厢号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="coachClass"
                    label="车厢等级"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="seatRowNumber"
                    label="车厢座位行数"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="seatColNumber"
                    label="车厢座位列数"
                    width="180">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" @click="dialogFormVisible1 = true;handleEdit(scope.$index, scope.row)">修改</el-button>
                        <el-button type="danger" @click="deleteById(scope.$index, scope.row)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>
        </el-table>
    </template>
    <div class="block">
        <el-pagination
                @current-change="handleCurrentChange"
                :current-page="currentPage1"
                layout="prev, pager, next"
                :page-size="pageSize"
                :total="totalCount"
        >
        </el-pagination>
    </div>

    <el-dialog title="创建车厢"
               :visible.sync="dialogFormVisible" width="30%"
               :close-on-click-modal="false"
               :before-close="cleanModel">
        <el-form :model="createForm" :rules="rules">
            <el-form-item label="列车号" :label-width="'100px'" prop="trainNum">

                <el-select v-model="createForm.trainNum" placeholder="请选择列车号">
                    <el-option
                            v-for="item in trains"
                            :key="item"
                            :label="item"
                            :value="item">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="车厢号" :label-width="'100px'" prop="coachNum">
                <el-input v-model="createForm.coachNum" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="车厢等级" :label-width="'100px'" prop="coachClass">
                <el-select v-model="createForm.coachClass" placeholder="请选择车厢等级">
                    <el-option
                            v-for="item in classes"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="车厢座位行数" :label-width="'110px'" prop="seatRowNumber">
                <el-input-number v-model="createForm.seatRowNumber" @change="handleChange" :min="1" :max="40" label="座位行数"></el-input-number>
            </el-form-item>
            <el-form-item label="车厢座位列数" :label-width="'110px'" prop="seatColNumber">
                <el-input-number v-model="createForm.seatColNumber" @change="handleChange" :min="1" :max="40" label="座位行数"></el-input-number>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cleanModel">取 消</el-button>
            <el-button type="primary" @click="create">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改车站信息"
               :visible.sync="dialogFormVisible1" width="30%"
               :close-on-click-modal="false"
               :before-close="cleanModel">
        <el-form :model="createForm" :rules="rules">
            <el-form-item label="列车号" :label-width="'100px'" prop="trainNum">

                <el-select v-model="createForm.trainNum" placeholder="请选择列车号">
                    <el-option
                            v-for="item in trains"
                            :key="item"
                            :label="item"
                            :value="item">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="车厢号" :label-width="'100px'" prop="coachNum">
                <el-input v-model="createForm.coachNum" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="车厢等级" :label-width="'100px'" prop="coachClass">

                <el-select v-model="createForm.coachClass" placeholder="请选择车厢等级">
                    <el-option
                            v-for="item in classes"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="车厢座位行数" :label-width="'110px'" prop="seatRowNumber">

                <el-input-number v-model="createForm.seatRowNumber" @change="handleChange" :min="1" :max="40" label="座位行数"></el-input-number>
            </el-form-item>
            <el-form-item label="车厢座位列数" :label-width="'110px'" prop="seatColNumber">

                <el-input-number v-model="createForm.seatColNumber" @change="handleChange" :min="1" :max="40" label="座位行数"></el-input-number>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cleanModel">取 消</el-button>
            <el-button type="primary" @click="update">确 定</el-button>
        </div>
    </el-dialog>

</div>
</body>

<script src="../../../js/axios-0.18.0.js"></script>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="../../js/request.js"></script>
<script>

    new Vue({
        el: '#app',
        mounted(){
            this.select();
            this.selectTrains();
        },
        data() {
            return {
                //activeIndex: '1',
                //activeIndex2: '1',
                dialogFormVisible: false,
                dialogFormVisible1: false,
                pageSize : 15,
                currentPage1 : 1,
                totalCount : 40,
                rules: {
                    trainNum: [
                        { required: true, message: '请输入列车号', trigger: 'blur' }
                    ],
                    coachNum: [
                        { required: true, message: '请输入列车车厢号', trigger: 'blur' }
                    ],
                    coachClass: [
                        { required: true, message: '请输入车厢等级', trigger: 'blur' }
                    ],
                    seatRowNumber: [
                        { required: true, message: '请输入座位行数', trigger: 'blur' }
                    ],
                    seatColNumber: [
                        { required: true, message: '请输入座位列数', trigger: 'blur' }
                    ],
                },
                trains :[
                    'G100','G101','G102'
                ],
                classes :[
                    {
                        value: 0,
                        label: '商务'
                    }, {
                        value: 1,
                        label: '一等'
                    }, {
                        value: 2,
                        label: '二等'
                    }, {
                        value: 3,
                        label: '棚车'
                    }
                ],
                searchForm : [{
                    trainNum: '',
                    coachNum: ''
                }],
                tableData: [{
                    id: '1',
                    trainNum: 'G100',
                    coachNum: '1车',
                    coachClass: '一等座',
                    seatRowNumber: '10',
                    seatColNumber: '4',
                    seatNumber: '40'
                }, {
                    id: '2',
                    trainNum: 'G100',
                    coachNum: '1车',
                    coachClass: '一等座',
                    seatRowNumber: '10',
                    seatColNumber: '4',
                    seatNumber: '40'
                }, {
                    id: '3',
                    trainNum: 'G100',
                    coachNum: '1车',
                    coachClass: '一等座',
                    seatRowNumber: '10',
                    seatColNumber: '4',
                    seatNumber: '40'
                }, {
                    id: '4',
                    trainNum: 'G100',
                    coachNum: '1车',
                    coachClass: '一等座',
                    seatRowNumber: '10',
                    seatColNumber: '4',
                    seatNumber: '40'
                }],
                createForm:{
                    trainNum: '',
                    coachNum: '',
                    coachClass: '',
                    seatRowNumber: '',
                    seatColNumber: ''
                },
                multipleSelection: [],
                selectedIds: []
            }
        },
        methods: {

            cleanModel(){
                this.createForm={
                    trainNum: '',
                    coachNum: '',
                    coachClass: '',
                    seatRowNumber: '',
                    seatColNumber: ''
                };
                this.selectedIds=[];
                this.dialogFormVisible=false;
                this.dialogFormVisible1=false;
            },

            selectAgain(){
                this.currentPage1=1;
                this.select();
            },

            handleCurrentChange(val) {
                this.currentPage1=val;
                this.select();
            },

            handleSelect(key, keyPath) {
                console.log(key, keyPath);
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleEdit(index,row){
                this.createForm={
                    id: this.tableData[index].id,
                    trainNum: this.tableData[index].trainNum,
                    coachNum: this.tableData[index].coachNum,
                    coachClass: this.tableData[index].coachClass,
                    seatRowNumber: this.tableData[index].seatRowNumber,
                    seatColNumber: this.tableData[index].seatColNumber
                };
                if (this.createForm.coachClass === '商务座') {
                    this.createForm.coachClass=0;
                }else if (this.createForm.coachClass === '一等座') {
                    this.createForm.coachClass=1;
                }
                else if (this.createForm.coachClass === '二等座') {
                    this.createForm.coachClass=2;
                }else if (this.createForm.coachClass === '棚车') {
                    this.createForm.coachClass=3;
                }
            },
            selectTrains(){
                var _this=this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/train/selectAll",
                }).then(function (resp) {
                    if(resp.data.code === 1){
                        _this.trains=resp.data.data;
                    }
                    else {
                        _this.$message({
                            showClose: true,
                            message: resp.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            select(){
                var _this=this;
                axios({
                    method:"get",
                    url:"http://localhost:8080/coach/page",
                    params: {
                        page: this.currentPage1,
                        trainNum: this.searchForm.trainNum,
                        coachNum: this.searchForm.coachNum
                    },

                }).then(function (resp) {
                    _this.tableData=resp.data.data.records;
                    _this.tableData.forEach(item => {
                        if (item.coachClass === 0) {
                            item.coachClass='商务座';
                        }else if (item.coachClass === 1) {
                            item.coachClass='一等座';
                        }
                        else if (item.coachClass === 2) {
                            item.coachClass='二等座';
                        }else if (item.coachClass === 3) {
                            item.coachClass='棚车';
                        }
                    });
                    _this.totalCount=resp.data.data.total;
                    _this.pageSize=resp.data.data.size;
                })
            },
            create(){
                var _this=this;
                axios({
                    method:"put",
                    url:"http://localhost:8080/coach/create",
                    data: _this.createForm
                }).then(function (resp) {
                    if(resp.data.code === 1){
                        _this.$message({
                            showClose: true,
                            message: '创建成功！',
                            type: 'success'
                        });
                        _this.dialogFormVisible=false;
                        _this.cleanModel();
                        _this.selectAgain();
                    }else {
                        _this.$message({
                            showClose: true,
                            message: resp.data.msg,
                            type: 'error'
                        });
                    }
                });

            },
            deleteById(index,row){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var id = this.tableData[index].id;
                    var _this = this;
                    axios({
                        method: "delete",
                        url: "http://localhost:8080/coach",
                        params: { id }
                    }).then(function (resp) {
                        if (resp.data.code === 1) {
                            _this.select();
                            _this.$message({
                                message: '删除成功！',
                                type: 'success'
                            });

                        }

                    })
                    this.cleanModel();
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            deleteByIds(){
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for(let i=0;i<this.multipleSelection.length;i++){
                        let selectionElement =this.multipleSelection[i];
                        this.selectedIds[i]=selectionElement.id;
                    }
                    var _this=this;
                    axios({
                        method:"post",
                        url:"http://localhost:8080/coach/deleteByIds",
                        data:_this.selectedIds
                    }).then(function (resp) {
                        if(resp.data.code===1){
                            _this.select();
                            _this.$message({
                                message: '删除成功！',
                                type: 'success'
                            });
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            update(){
                var _this=this;
                axios({
                    method:"put",
                    url:"http://localhost:8080/coach/update",
                    data: _this.createForm
                }).then(function (resp) {
                    if(resp.data.code === 1){
                        _this.$message({
                            showClose: true,
                            message: '修改成功！',
                            type: 'success'
                        });
                        _this.dialogFormVisible1=false;
                        _this.cleanModel();
                        _this.select();

                    }else {
                        _this.$message({
                            showClose: true,
                            message: '修改失败',
                            type: 'error'
                        });
                    }
                });

            }
        }
    })
</script>



</html>